<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      size="small"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="参数名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column
        type="index"
        align="center"
        label="编号"
        width="80"
      ></el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="场次名称"
      ></el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        label="每日开始时间"
      ></el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="每日结束时间"
      ></el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="启用状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="small" type="danger"
            >禁用</el-tag
          >
          <el-tag v-else size="small">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
      ></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="relationProduct(scope.row.id)"
            >关联商品</el-button
          >
          <br />
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status === 0"
            type="text"
            size="small"
            @click="enableHandle(scope.row)"
            >禁用</el-button
          >
          <el-button
            v-if="scope.row.status === 1"
            type="text"
            size="small"
            @click="disableHandle(scope.row)"
            >启用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <div>
      <el-dialog
        append-to-body
        :close-on-click-modal="false"
        :visible.sync="visible"
        title="关联秒杀商品"
        width="60%"
      >
        <seckillsku-relation
          ref="seckillskuRelation"
          :sessionId="currentId"
        ></seckillsku-relation>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import AddOrUpdate from "./seckillsession-add-or-update";
import SeckillskuRelation from "./seckillskurelation";
import {
  pageSeckillSessionList,
  removeSeckillSession,
  enableSeckillSession,
  disableSeckillSession,
} from "@/api/coupon";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      visible: false,
      currentId: 0,
    };
  },
  components: {
    AddOrUpdate,
    SeckillskuRelation,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      pageSeckillSessionList({
        pageNumber: this.pageIndex,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = res.data.data;
          this.totalPage = res.data.count;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    getRealtionDataList() {},
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    relationProduct(id) {
      this.visible = true;
      this.currentId = id;
      this.$nextTick(() => {
        this.$refs.seckillskuRelation.getDataList();
      });
    },
    enableHandle(row) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enableSeckillSession({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(row) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disableSeckillSession({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定是否进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeSeckillSession({ ids: ids }).then((res) => {
          if (res && res.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },
  },
};
</script>
